package com.slkj.shilixiaoyuanapp.ui.teacher.tool.MyApproval;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.entity.KqSsDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.ChosePicAdapter;
import com.slkj.shilixiaoyuanapp.view.DividerItemDecoration;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.StateLayout;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.CHOSE_PIC_SPAN_COUNT;

@ActivityInfo(layout = R.layout.activity_sp_kq_ss, title = "申诉")
public class SpKqSsActivity extends BaseActivity {
    @InjectParam
    int aId;
    @InjectParam
    int id;
    @BindView(R.id.tv_ycqk)
    TextView tvYcqk;
    @BindView(R.id.statelayout)
    StateLayout stateLayout;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.recyc_pic)
    RecyclerView recycerPic;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.recycer_spr)
    RecyclerView recycerSpr;
    @BindView(R.id.layout_botton)
    View viewBotton;
    @BindView(R.id.layout_reson)
    View viewReson;

    @BindView(R.id.edit_reson)
    EditText editReson;
    SqrAdapter sqrAdapter;
    ChosePicAdapter adapter;
    ArrayList<String> picPaths = new ArrayList<>();

    KqSsDetailEntity data;
    @Override
    protected void init() {
        SpKqSsActivity__JumpCenter.bind(this);
         recycerPic.setLayoutManager(new GridLayoutManager(this, CHOSE_PIC_SPAN_COUNT));
        adapter = new ChosePicAdapter(picPaths, this);
        recycerPic.setAdapter(adapter);
        HttpHeper.get().toolService()
                .getKqSsDetailByMessage(aId)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<KqSsDetailEntity>(stateLayout) {
                    @Override
                    public void onCallBackSuccess(KqSsDetailEntity data) {
                        //填充数据
                        seBViewData(data);

                    }
                });
    }

    private void seBViewData(KqSsDetailEntity data) {
        this.data = data;
        tvYcqk.setText(data.getTitle());
        tvTime.setText(data.getDatetime());
        editContent.setText(data.getReason());
        if (data.getImages() != null && !data.getImages().isEmpty()) {
            recycerPic.setLayoutManager(new GridLayoutManager(SpKqSsActivity.this, CHOSE_PIC_SPAN_COUNT));
            adapter = new ChosePicAdapter(data.getImages(), SpKqSsActivity.this, false);
            recycerPic.setAdapter(adapter);
        }
        editReson.setText(data.getRefute());

        if (data.getAuditor() != null && !data.getAuditor().isEmpty()){
            recycerSpr.addItemDecoration(new DividerItemDecoration(this,LinearLayoutManager.HORIZONTAL));
            recycerSpr.setLayoutManager(new LinearLayoutManager(this));

            sqrAdapter = new SqrAdapter(data.getAuditor());
            recycerSpr.setAdapter(sqrAdapter);
        }
        switch (data.getState()){
            case MyApprovalHistoryFragment.TYPE_NOT_LOOK:
                viewBotton.setVisibility(View.VISIBLE);
                break;
            case MyApprovalHistoryFragment.TYPE_REFUSE:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
            case MyApprovalHistoryFragment.TYPE_OK:
                viewBotton.setVisibility(View.GONE);
                editReson.setFocusable(false);
                break;
        }
    }
    @OnClick(R.id.tv_tg)void tg(){
        String reson = editReson.getText().toString();
        upData(reson,2);

    }

    private void upData(String reson,int state) {
        HttpHeper.get().toolService()
                .spDo(id,aId,state,reson)
                .compose(getThread())
                .compose(bindToLifecycle())
                .subscribe(new CommonCallBack<String>(true, this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        //showToast(data);
                        LoadSuccessAndFailDialog.showSuccess(SpKqSsActivity.this,data);
                        finish();
                    }
                });
    }

    @OnClick(R.id.tv_wtg)void wtg(){
        String reson = editReson.getText().toString();
        if (TextUtils.isEmpty(reson)){
            showToast("请填写拒绝原因");
            return;
        }
        upData(reson,1);
    }

}
